﻿using Module_Debug.Hardware;
using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug
{
    public partial class DemuraAutoMeasure : Form
    {
        CRC16 CRCData = new CRC16();
        private byte[] Send_buf = new byte[1];


        //CA310 测量数据
        CaSDK Ca310Obj = new CaSDK();
        public double auto_lum;//
        private double auto_x;
        private double auto_y;

        private void Msr_CA310()
        {
            MainForm.Msr_CASDK();
            auto_lum = MainForm.CASDK_LUM;
            auto_x = MainForm.CASDK_x;
            auto_y = MainForm.CASDK_y;
        }
        private double auto_lum1;//
        private double auto_x1;
        private double auto_y1;
        private double auto_lum2;//
        private double auto_x2;
        private double auto_y2;
        private void Msr_CA310_OFF()
        {
            MainForm.Msr_CASDK();
            auto_lum1 = MainForm.CASDK_LUM;
            auto_x1 = MainForm.CASDK_x;
            auto_y1 = MainForm.CASDK_y;
        }
        private void Msr_CA310_ON()
        {
            MainForm.Msr_CASDK();
            auto_lum2 = MainForm.CASDK_LUM;
            auto_x2 = MainForm.CASDK_x;
            auto_y2 = MainForm.CASDK_y;

        }
        frmMain MainForm = null;
        public DemuraAutoMeasure(frmMain Frm)
        {
            MainForm = Frm;
            InitializeComponent();
        }
        private void btn_Demura_AutoMeasure_Synt_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            switch (btnTmp.Name)
            {
                case "btn_De_OnOff_Auto_Single":
                    {
                        //MainForm.Demura_Click(MainForm.Demura_Function_OFF, e);
                        Thread.Sleep(650);
                        Msr_CA310_OFF();
                        //MainForm.Demura_Click(MainForm.Demura_Function_ON, e);
                        Thread.Sleep(650);
                        Msr_CA310_ON();
                        Dgv_Demura_AutoMeasure_Information(auto_lum1, auto_x1, auto_y1, auto_lum2, auto_x2, auto_y2);
                    }
                    break;
                case "btn_De_OnOff_SaveAs"://重新配置存demura on/off 数据
                    {   //保存
                        SaveFileDialog sf = new SaveFileDialog(); //对话框
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

                        string str = "序号,灰阶,De_Off_x坐标,De_Off_y坐标,De_Off_Lv亮度,De_On_x坐标,De_On_y坐标,De_On_Lv亮度,De_Off/on_x坐标偏差,De_Off/on_y坐标偏差,De_Off/on_Lv亮度偏差\r\n";
                        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                        {
                            if (sf.FileName == "") return; //如果没有输入文件名
                            try
                            {
                                StreamWriter sw = null; //文件流
                                sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                                for (int i = 0; i < (this.Dgv_Demura_AutoMeasure.Rows.Count - 1); i++)
                                {
                                    str = str + this.Dgv_Demura_AutoMeasure.Rows[i].Cells[0].Value.ToString() + ","
                                        + this.Dgv_Demura_AutoMeasure.Rows[i].Cells[1].Value.ToString() + ","
                                        + this.Dgv_Demura_AutoMeasure.Rows[i].Cells[2].Value.ToString() + ","
                                        + this.Dgv_Demura_AutoMeasure.Rows[i].Cells[3].Value.ToString() + ","
                                        + this.Dgv_Demura_AutoMeasure.Rows[i].Cells[4].Value.ToString() + ","
                                        + this.Dgv_Demura_AutoMeasure.Rows[i].Cells[5].Value.ToString() + ","
                                        + this.Dgv_Demura_AutoMeasure.Rows[i].Cells[6].Value.ToString() + ","
                                        + this.Dgv_Demura_AutoMeasure.Rows[i].Cells[7].Value.ToString() + ","
                                        + this.Dgv_Demura_AutoMeasure.Rows[i].Cells[8].Value.ToString() + ","
                                        + this.Dgv_Demura_AutoMeasure.Rows[i].Cells[9].Value.ToString() + ","
                                        + this.Dgv_Demura_AutoMeasure.Rows[i].Cells[10].Value.ToString() + "\r\n";

                                }
                                sw.WriteLine(str);
                                sw.Dispose(); //释放资源
                                sw.Close(); //关闭数据流
                                MessageBox.Show("CA310数据保存OK！", "系统提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据保存时发生错误，请再试！", "系统提示：");
                            }
                        }
                    }
                    break;
                case "btn_De_OnOff_Clear":
                    {
                        DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                        if (dr == DialogResult.OK)
                        {
                            this.Dgv_Demura_AutoMeasure.Rows.Clear();
                        }
                        else {; }
                    }
                    break;
                case "btn_De_OnOff_Del":
                    {
                        int Current_Index, Row_Accum;
                        Current_Index = this.Dgv_Demura_AutoMeasure.SelectedCells[0].RowIndex;
                        DataGridViewRow Row1 = Dgv_Demura_AutoMeasure.Rows[Current_Index];
                        this.Dgv_Demura_AutoMeasure.Rows.Remove(Row1);
                        this.Dgv_Demura_AutoMeasure.Rows[Current_Index].Selected = false;
                        Row_Accum = this.Dgv_Demura_AutoMeasure.Rows.Count - 1;
                        for (Current_Index = 0; Current_Index < Row_Accum; Current_Index++)
                        {
                            this.Dgv_Demura_AutoMeasure.Rows[Current_Index].Cells[0].Value = Current_Index + 1;//序号
                        }
                        this.Dgv_Demura_AutoMeasure.Rows[Row_Accum - 1].Selected = true;
                        Dgv_Demura_AutoMeasure.FirstDisplayedScrollingRowIndex = Dgv_Demura_AutoMeasure.RowCount - 1;
                    }
                    break;
                default: break;
            }
        }
        private void Demura_AutoMeasure_GraySelect_Enable(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            RadioButton rbtnTmp = (RadioButton)sender;
            Setting.TempRecord.cmdLen = 25;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            switch (rbtnTmp.Name)
            {

                case "rbtn_Gray255_Point":                             //写入Gray255
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0xFF;
                    }
                    break;
                case "rbtn_Gray128_Point":                            //写入Gray128
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0x80;
                        Send_buf[4] = 0x80;
                        Send_buf[5] = 0x80;
                    }
                    break;
                case "rbtn_Gray64_Point":                            //写入Gray64
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0x40;
                        Send_buf[4] = 0x40;
                        Send_buf[5] = 0x40;
                    }
                    break;
                case "rbtn_Gray32_Point":                            //写入Gray32
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0x20;
                        Send_buf[4] = 0x20;
                        Send_buf[5] = 0x20;
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 25);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            Thread.Sleep(800);
        }
        private void btn_De_OnOff_0Cal_Click(object sender, EventArgs e)
        {
            try
            {
                Tim1Ca310.Start();//显示提示310连接提示信息
                MainForm.textBox1.Text += "CA310 连接校准中...\r\n";
                MainForm.GrayShow(0, 0, 0);
            }
            catch
            {
                Thread.Sleep(100);
            }

            try
            {
                //btnCal_manual.Text = "Wait";
                Ca310Obj.CA310_Connect();
                Ca310Obj.CA310_Initial(byte.Parse(MainForm.CA310_Channel.Text));

                Dgv_Demura_AutoMeasure.Enabled = true;//激活Demura_AutoMeasure数据表格窗口
                btn_De_OnOff_Del.Enabled = true;//删除                
                btn_De_OnOff_Clear.Enabled = true;//清空
                btn_De_OnOff_SaveAs.Enabled = true;//导出
                btn_De_Cover_Measure.Enabled = true;//全部测量
                btn_De_OnOff_Auto_Single.Enabled = true;//单次测量
                btn_Red_Raw_Measure.Enabled = true;//RAW_RED测量
                btn_Green_Raw_Measure.Enabled = true;//RAW_Green测量
                btn_Blue_Raw_Measure.Enabled = true;//RAW_Blue测量
                btn_White_Raw_Measure.Enabled = true;//RAW_White测量
                btn_NineUnf_G255_Enable();
            }
            catch (Exception EX)
            {
                MessageBox.Show("CA310 Connect Fail!!" + EX.Message);
            }
            btn_De_OnOff_0Cal.Text = "0C";
            btn_De_OnOff_0Cal.BackColor = Color.Gray;
            MainForm.textBox1.Text += "CA310 连接完成...\r\n";
        }
        private int[] GraySelectAll = new int[4] { 255, 128, 64, 32 };
        private void Demura_AutoMeasure_GraySelectAll_Enable(object sender, EventArgs e)
        {
            Send_buf = new byte[25];

            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            for (int i = 0; i < GraySelectAll.Length; i++)
            {
                if (btnTmp.Name == "btn_De_Cover_Measure")
                {
                    Send_buf[1] = 0x07;
                    Send_buf[3] = Convert.ToByte(GraySelectAll[i]);
                    Send_buf[4] = Convert.ToByte(GraySelectAll[i]);
                    Send_buf[5] = Convert.ToByte(GraySelectAll[i]);
                    CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
                    try
                    {
                        MainForm.comPort.Write(Send_buf, 0, 25);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }
                    Thread.Sleep(800);
                    //-------------MainForm.Demura_Click(MainForm.Demura_Function_OFF,e);  
                    Thread.Sleep(650);
                    Msr_CA310_OFF();
                    //--------------MainForm.Demura_Click(MainForm.Demura_Function_ON,e);
                    MessageBox.Show("代码整理，请针对源码修改", "错误");
                    Thread.Sleep(650);
                    Msr_CA310_ON();
                    Dgv_Demura_AutoMeasure_Information(auto_lum1, auto_x1, auto_y1, auto_lum2, auto_x2, auto_y2);
                    Thread.Sleep(400);
                }
                else
                {
                    Send_buf[1] = 0x07;
                    Send_buf[3] = 0x00;
                    Send_buf[4] = 0x00;
                    Send_buf[5] = 0x00;
                    CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
                    try
                    {
                        MainForm.comPort.Write(Send_buf, 0, 25);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }
                    Thread.Sleep(800);
                }
            }
        }
        private void Dgv_Demura_AutoMeasure_Information(double Dgv_De_Off_lum, double Dgv_De_Off_x, double Dgv_De_Off_y, double Dgv_De_On_lum, double Dgv_De_On_x, double Dgv_De_On_y) //表格数据显示
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;

            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.Dgv_Demura_AutoMeasure.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.Dgv_Demura_AutoMeasure.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.Dgv_Demura_AutoMeasure.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.Dgv_Demura_AutoMeasure.Rows[Row_Accum - 1].Selected = false;
                        this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.Dgv_Demura_AutoMeasure.Rows.Add(Row1);
            }
            this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Cells[1].Value = Row_Accum + 1;//GraySelectAll[i];//写入相应灰阶调试？
            this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Cells[2].Value = Dgv_De_Off_x.ToString("#0.0000");
            this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Cells[3].Value = Dgv_De_Off_y.ToString("#0.0000");
            this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Cells[4].Value = Dgv_De_Off_lum.ToString("#0.0000");
            this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Cells[5].Value = Dgv_De_On_x.ToString("#0.0000");
            this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Cells[6].Value = Dgv_De_On_y.ToString("#0.0000");
            this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Cells[7].Value = Dgv_De_On_lum.ToString("#0.0000");
            this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Cells[8].Value = (Dgv_De_On_x - Dgv_De_Off_x).ToString("#0.0000");
            this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Cells[9].Value = (Dgv_De_On_y - Dgv_De_Off_y).ToString("#0.0000");
            this.Dgv_Demura_AutoMeasure.Rows[Row_Accum].Cells[10].Value = Math.Round((Dgv_De_On_lum - Dgv_De_Off_lum) / Dgv_De_Off_lum, 4);//无法进行百分比输出；
            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.Dgv_Demura_AutoMeasure.Rows.Count - 1;
                this.Dgv_Demura_AutoMeasure.Rows[Current_Index].Selected = false;
                this.Dgv_Demura_AutoMeasure.Rows[Row_Accum - 1].Selected = true;
            }
            Dgv_Demura_AutoMeasure.FirstDisplayedScrollingRowIndex = Dgv_Demura_AutoMeasure.RowCount - 1;
        }
        /**************九点均一性自动测量************/
        #region //==========NinePoint Measure Click===========
        private void btn_NineUnf_G255_Enable()
        {
            //255
            btn_NineUnf_G255_11.Enabled = true; btn_NineUnf_G255_12.Enabled = true; btn_NineUnf_G255_13.Enabled = true;
            btn_NineUnf_G255_21.Enabled = true; btn_NineUnf_G255_22.Enabled = true; btn_NineUnf_G255_23.Enabled = true;
            btn_NineUnf_G255_31.Enabled = true; btn_NineUnf_G255_32.Enabled = true; btn_NineUnf_G255_33.Enabled = true;
            //128
            btn_NineUnf_G128_11.Enabled = true; btn_NineUnf_G128_12.Enabled = true; btn_NineUnf_G128_13.Enabled = true;
            btn_NineUnf_G128_21.Enabled = true; btn_NineUnf_G128_22.Enabled = true; btn_NineUnf_G128_23.Enabled = true;
            btn_NineUnf_G128_31.Enabled = true; btn_NineUnf_G128_32.Enabled = true; btn_NineUnf_G128_33.Enabled = true;
            //64
            btn_NineUnf_G64_11.Enabled = true; btn_NineUnf_G64_12.Enabled = true; btn_NineUnf_G64_13.Enabled = true;
            btn_NineUnf_G64_21.Enabled = true; btn_NineUnf_G64_22.Enabled = true; btn_NineUnf_G64_23.Enabled = true;
            btn_NineUnf_G64_31.Enabled = true; btn_NineUnf_G64_32.Enabled = true; btn_NineUnf_G64_33.Enabled = true;
            //32
            btn_NineUnf_G32_11.Enabled = true; btn_NineUnf_G32_12.Enabled = true; btn_NineUnf_G32_13.Enabled = true;
            btn_NineUnf_G32_21.Enabled = true; btn_NineUnf_G32_22.Enabled = true; btn_NineUnf_G32_23.Enabled = true;
            btn_NineUnf_G32_31.Enabled = true; btn_NineUnf_G32_32.Enabled = true; btn_NineUnf_G32_33.Enabled = true;
        }
        private void tab_Ninepoint_Uniformity_SelectedIndexChanged(object sender, EventArgs e)//TabPageChange Gray切换
        {
            Send_buf = new byte[25];
            TabControl tabTmp = (TabControl)sender;
            Setting.TempRecord.cmdLen = 25;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            if (tabTmp.SelectedTab == tabp_G255)//TabPage写入Gray255;
            {
                //MessageBox.Show("请确认上一灰阶数据是否记录！");//255
                txt_NineUnfDa_G255_Clear();//FF
                Send_buf[1] = 0x07;
                Send_buf[3] = 0xDA;
                Send_buf[4] = 0xDA;
                Send_buf[5] = 0xDA;
                txt_Statistics_Gray.Text = "218"/*Convert.ToString(255)*/;
            }
            else if (tabTmp.SelectedTab == tabp_G128)//TabPage写入Gray128;
            {
                //MessageBox.Show("请确认上一灰阶数据是否记录！");//80
                txt_NineUnfDa_G255_Clear();
                Send_buf[1] = 0x07;
                Send_buf[3] = 0x64;
                Send_buf[4] = 0x64;
                Send_buf[5] = 0x64;
                txt_Statistics_Gray.Text = "100"/*Convert.ToString(128)*/;

            }
            else if (tabTmp.SelectedTab == tabp_G64) //TabPage写入Gray64                                                     
            {
                //MessageBox.Show("请确认上一灰阶数据是否记录！");//40
                txt_NineUnfDa_G255_Clear();
                Send_buf[1] = 0x07;
                Send_buf[3] = 0x24;
                Send_buf[4] = 0x24;
                Send_buf[5] = 0x24;
                txt_Statistics_Gray.Text = "36"/*Convert.ToString(64)*/;

            }
            else if (tabTmp.SelectedTab == tabp_G32) //TabPage写入Gray64 
            {
                //MessageBox.Show("请确认上一灰阶数据是否记录！");//20
                txt_NineUnfDa_G255_Clear();
                Send_buf[1] = 0x07;
                Send_buf[3] = 0x08;
                Send_buf[4] = 0x08;
                Send_buf[5] = 0x08;
                txt_Statistics_Gray.Text = "8"/*Convert.ToString(32)*/;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 25);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            Thread.Sleep(800);
        }
        private int i = 0;
        private void tab_NineUnfData_SelectedIndexChanged(object sender, EventArgs e)
        {
            TabControl tabTmp = (TabControl)sender;
            if (tabTmp.SelectedTab == tab_NinePoint_Data)
            {
                i = 0;
                Dgv_NinePoint_Measure_Information(); //Nine Point表格数据显示
            }
            else if (tabTmp.SelectedTab == tab_NinePoint_Lv)
            {
                Dgv_NinePoint_Measure.Rows.Clear(); //Nine Point表格数据显示
            }
            else if (tabTmp.SelectedTab == tab_NinePoint_Xa)
            {
                Dgv_NinePoint_Measure.Rows.Clear(); //Nine Point表格数据显示
            }
            else if (tabTmp.SelectedTab == tab_NinePoint_Ya)
            {
                Dgv_NinePoint_Measure.Rows.Clear(); //Nine Point表格数据显示
            }

        }
        private void txt_NineUnfDa_G255_Clear()
        {
            txt_NineUnfDa_G255_Lv11.Text = "0.0000";
            txt_NineUnfDa_G255_Lv12.Text = "0.0000";
            txt_NineUnfDa_G255_Lv13.Text = "0.0000";

            txt_NineUnfDa_G255_Lv21.Text = "0.0000";
            txt_NineUnfDa_G255_Lv22.Text = "0.0000";
            txt_NineUnfDa_G255_Lv23.Text = "0.0000";

            txt_NineUnfDa_G255_Lv31.Text = "0.0000";
            txt_NineUnfDa_G255_Lv32.Text = "0.0000";
            txt_NineUnfDa_G255_Lv33.Text = "0.0000";
        }
        private double[] btn_NinePointName_LvValue = new double[9];
        private double[] btn_NinePointName_XaValue = new double[9];
        private double[] btn_NinePointName_YaValue = new double[9];
        private void btn_NinePoint_Uniformity_Sync_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            switch (btnTmp.Name)
            {
                #region//灰阶选择
                case "btn_NineUnf_G255_11":   //btn Point 11             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv11.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa11.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya11.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G255_12":   //btn Point 12             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv12.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa12.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya12.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G255_13":   //btn Point 13             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv13.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa13.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya13.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G255_21":   //btn Point 21             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv21.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa21.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya21.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G255_22":   //btn Point 22             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv22.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa22.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya22.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G255_23":   //btn Point 23             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv23.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa23.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya23.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G255_31":   //btn Point 31             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv31.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa31.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya31.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G255_32":   //btn Point 32             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv32.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa32.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya32.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G255_33":   //btn Point 33             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv33.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa33.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya33.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G128_11":   //btn Point 11             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv11.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa11.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya11.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G128_12":   //btn Point 12             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv12.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa12.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya12.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G128_13":   //btn Point 13             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv13.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa13.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya13.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G128_21":   //btn Point 21             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv21.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa21.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya21.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G128_22":   //btn Point 22             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv22.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa22.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya22.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G128_23":   //btn Point 23             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv23.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa23.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya23.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G128_31":   //btn Point 31             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv31.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa31.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya31.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G128_32":   //btn Point 32             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv32.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa32.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya32.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G128_33":   //btn Point 33             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv33.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa33.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya33.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G64_11":   //btn Point 11             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv11.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa11.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya11.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G64_12":   //btn Point 12             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv12.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa12.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya12.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G64_13":   //btn Point 13             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv13.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa13.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya13.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G64_21":   //btn Point 21             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv21.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa21.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya21.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G64_22":   //btn Point 22             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv22.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa22.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya22.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G64_23":   //btn Point 23             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv23.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa23.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya23.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G64_31":   //btn Point 31             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv31.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa31.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya31.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G64_32":   //btn Point 32             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv32.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa32.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya32.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G64_33":   //btn Point 33             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv33.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa33.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya33.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G32_11":   //btn Point 11             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv11.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa11.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya11.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G32_12":   //btn Point 12             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv12.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa12.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya12.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G32_13":   //btn Point 13             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv13.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa13.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya13.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G32_21":   //btn Point 21             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv21.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa21.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya21.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G32_22":   //btn Point 22             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv22.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa22.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya22.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G32_23":   //btn Point 23             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv23.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa23.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya23.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G32_31":   //btn Point 31             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv31.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa31.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya31.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G32_32":   //btn Point 32             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv32.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa32.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya32.Text = auto_y.ToString("F4");
                    }
                    break;
                case "btn_NineUnf_G32_33":   //btn Point 33             
                    {
                        Msr_CA310();
                        txt_NineUnfDa_G255_Lv33.Text = auto_lum.ToString("F4");
                        txt_NineUnfDa_G255_Xa33.Text = auto_x.ToString("F4");
                        txt_NineUnfDa_G255_Ya33.Text = auto_y.ToString("F4");
                    }
                    break;
                default:
                    break;
            }
            #endregion
            txt_NineUnfDa_LvMax.Text = Convert.ToString(NinePoint_UnifDaMeasure_Max_Extract(Convert.ToDouble(txt_NineUnfDa_G255_Lv11.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv12.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv13.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Lv21.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv22.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv23.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Lv31.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv32.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv33.Text)));
            txt_NineUnfDa_LvMin.Text = Convert.ToString(NinePoint_UnifDaMeasure_Min_Extract(Convert.ToDouble(txt_NineUnfDa_G255_Lv11.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv12.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv13.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Lv21.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv22.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv23.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Lv31.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv32.Text), Convert.ToDouble(txt_NineUnfDa_G255_Lv33.Text)));
            txt_NineUnfDa_LvUnf.Text = Convert.ToString(Math.Round(Convert.ToDouble(txt_NineUnfDa_LvMin.Text) / Convert.ToDouble(txt_NineUnfDa_LvMax.Text) * 100, 4)) + "%";

            txt_NineUnfDa_XaMax.Text = Convert.ToString(NinePoint_UnifDaMeasure_Max_Extract(Convert.ToDouble(txt_NineUnfDa_G255_Xa11.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa12.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa13.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Xa21.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa22.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa23.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Xa31.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa32.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa33.Text)));
            txt_NineUnfDa_XaMin.Text = Convert.ToString(NinePoint_UnifDaMeasure_Min_Extract(Convert.ToDouble(txt_NineUnfDa_G255_Xa11.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa12.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa13.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Xa21.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa22.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa23.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Xa31.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa32.Text), Convert.ToDouble(txt_NineUnfDa_G255_Xa33.Text)));
            txt_NineUnfDa_XaDev.Text = Convert.ToString(Math.Round(Convert.ToDouble(txt_NineUnfDa_XaMax.Text) - Convert.ToDouble(txt_NineUnfDa_XaMin.Text), 4));

            txt_NineUnfDa_YaMax.Text = Convert.ToString(NinePoint_UnifDaMeasure_Max_Extract(Convert.ToDouble(txt_NineUnfDa_G255_Ya11.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya12.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya13.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Ya21.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya22.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya23.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Ya31.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya32.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya33.Text)));
            txt_NineUnfDa_YaMin.Text = Convert.ToString(NinePoint_UnifDaMeasure_Min_Extract(Convert.ToDouble(txt_NineUnfDa_G255_Ya11.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya12.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya13.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Ya21.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya22.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya23.Text),
                                                                                            Convert.ToDouble(txt_NineUnfDa_G255_Ya31.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya32.Text), Convert.ToDouble(txt_NineUnfDa_G255_Ya33.Text)));
            txt_NineUnfDa_YaDev.Text = Convert.ToString(Math.Round(Convert.ToDouble(txt_NineUnfDa_YaMax.Text) - Convert.ToDouble(txt_NineUnfDa_YaMin.Text), 4));

            btn_NinePointName_LvValue[0] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Lv11.Text), 4); btn_NinePointName_LvValue[1] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Lv12.Text), 4); btn_NinePointName_LvValue[2] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Lv13.Text), 4);
            btn_NinePointName_LvValue[3] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Lv21.Text), 4); btn_NinePointName_LvValue[4] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Lv22.Text), 4); btn_NinePointName_LvValue[5] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Lv23.Text), 4);
            btn_NinePointName_LvValue[6] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Lv31.Text), 4); btn_NinePointName_LvValue[7] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Lv32.Text), 4); btn_NinePointName_LvValue[8] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Lv33.Text), 4);

            btn_NinePointName_XaValue[0] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Xa11.Text), 4); btn_NinePointName_XaValue[1] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Xa12.Text), 4); btn_NinePointName_XaValue[2] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Xa13.Text), 4);
            btn_NinePointName_XaValue[3] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Xa21.Text), 4); btn_NinePointName_XaValue[4] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Xa22.Text), 4); btn_NinePointName_XaValue[5] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Xa23.Text), 4);
            btn_NinePointName_XaValue[6] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Xa31.Text), 4); btn_NinePointName_XaValue[7] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Xa32.Text), 4); btn_NinePointName_XaValue[8] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Xa33.Text), 4);

            btn_NinePointName_YaValue[0] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Ya11.Text), 4); btn_NinePointName_YaValue[1] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Ya12.Text), 4); btn_NinePointName_YaValue[2] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Ya13.Text), 4);
            btn_NinePointName_YaValue[3] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Ya21.Text), 4); btn_NinePointName_YaValue[4] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Ya22.Text), 4); btn_NinePointName_YaValue[5] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Ya23.Text), 4);
            btn_NinePointName_YaValue[6] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Ya31.Text), 4); btn_NinePointName_YaValue[7] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Ya32.Text), 4); btn_NinePointName_YaValue[8] = Math.Round(Convert.ToDouble(txt_NineUnfDa_G255_Ya33.Text), 4);

        }
        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)//九点均一性采集数据图表另存为
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

            string str = "No.,Gray,Lv,X,Y\r\n";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    for (int i = 0; i < (this.Dgv_NinePoint_Measure.Rows.Count - 1); i++)
                    {
                        str = str + this.Dgv_NinePoint_Measure.Rows[i].Cells[0].Value.ToString() + ","
                            + this.Dgv_NinePoint_Measure.Rows[i].Cells[1].Value.ToString() + ","
                            + this.Dgv_NinePoint_Measure.Rows[i].Cells[2].Value.ToString() + ","
                            + this.Dgv_NinePoint_Measure.Rows[i].Cells[3].Value.ToString() + ","
                            + this.Dgv_NinePoint_Measure.Rows[i].Cells[4].Value.ToString() + "\r\n";

                    }
                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("九点均一性测量数据保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("九点均一性测量保存时发生错误，请再试！", "系统提示：");
                }
            }
        }
        #endregion
        #region//============极值函数调用及绘表==============
        private double NinePoint_UnifDaMeasure_Max_Extract(double x1, double x2, double x3, double x4, double x5, double x6, double x7, double x8, double x9)//测量数据最大提取  double x1, double x2,double x3, double x4, double x5, double x6, double x7, double x8, double x9
        {
            double[] Data = new double[9] { x1, x2, x3, x4, x5, x6, x7, x8, x9 };
            double MaxData = Data[0];
            foreach (double item in Data)
            {
                if (item > MaxData)
                    MaxData = item;
            }
            return MaxData;
        }
        private double NinePoint_UnifDaMeasure_Min_Extract(double x1, double x2, double x3, double x4, double x5, double x6, double x7, double x8, double x9)//测量数据最小值提取  double x1, double x2,double x3, double x4, double x5, double x6, double x7, double x8, double x9
        {

            double[] Data = new double[9] { x1, x2, x3, x4, x5, x6, x7, x8, x9 };
            double MinData = Data[0];
            foreach (double item in Data)
            {
                if (item < MinData)
                    MinData = item;
            }

            return MinData;

        }
        private void Dgv_NinePoint_Measure_Information() //Nine Point表格数据显示
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;
            for (i = 0; i < 9; i++)
            {
                DataGridViewRow Row1 = new DataGridViewRow();
                Row_Accum = this.Dgv_NinePoint_Measure.Rows.Count - 1;
                if (Row_Accum != 0)
                {
                    Current_Index = this.Dgv_NinePoint_Measure.SelectedCells[0].RowIndex;
                    if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                    {
                        Row_Accum = Current_Index;
                        Row_flag = 1;
                    }
                    else
                    {
                        this.Dgv_NinePoint_Measure.Rows.Add(Row1);
                    }
                    if ((Row_Accum != 0) && (Row_flag != 1))
                    {
                        if (Current_Index != Row_Accum)
                        {
                            this.Dgv_NinePoint_Measure.Rows[Row_Accum - 1].Selected = false;
                            this.Dgv_NinePoint_Measure.Rows[Row_Accum].Selected = true;

                        }
                    }
                }
                else
                {
                    this.Dgv_NinePoint_Measure.Rows.Add(Row1);
                }
                this.Dgv_NinePoint_Measure.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;//序号
                //Rows[x]为第x行,Cells[x]为第x列
                this.Dgv_NinePoint_Measure.Rows[Row_Accum].Cells[1].Value = Convert.ToDouble(txt_Statistics_Gray.Text);//GraySelectAll[i];//写入相应灰阶调试？
                this.Dgv_NinePoint_Measure.Rows[Row_Accum].Cells[2].Value = btn_NinePointName_LvValue[i];
                this.Dgv_NinePoint_Measure.Rows[Row_Accum].Cells[3].Value = btn_NinePointName_XaValue[i];
                this.Dgv_NinePoint_Measure.Rows[Row_Accum].Cells[4].Value = btn_NinePointName_YaValue[i];
                this.Dgv_NinePoint_Measure.Rows[Row_Accum].Cells[5].Value = btn_NinePointName_LvValue[i]/*-btn_NinePointName_LvValue[i+1];*/;
                this.Dgv_NinePoint_Measure.Rows[Row_Accum].Cells[6].Value = btn_NinePointName_XaValue[i]/*-btn_NinePointName_XaValue[i + 1]*/;
                this.Dgv_NinePoint_Measure.Rows[Row_Accum].Cells[7].Value = btn_NinePointName_YaValue[i]/*-btn_NinePointName_YaValue[i + 1]*/;

                if (Row_flag == 1)
                {
                    Row_flag = 0;
                    Row_Accum = this.Dgv_NinePoint_Measure.Rows.Count - 1;
                    this.Dgv_NinePoint_Measure.Rows[Current_Index].Selected = false;
                    this.Dgv_NinePoint_Measure.Rows[Row_Accum - 1].Selected = true;
                }
                Dgv_NinePoint_Measure.FirstDisplayedScrollingRowIndex = Dgv_NinePoint_Measure.RowCount - 1;
            }
        }
        #endregion
        /*************Demura Raw Data采集************/
        #region//=========Raw RGB Data Measure Click=========
        public static int[] GrayRGBSelectAll = new int[9] { 255, 224, 192, 160, 128, 96, 64, 32, 0 };//取样灰阶；
        public static double[] Daw_Red_Y_Axie = new double[9];
        public static double[] Daw_Green_Y_Axie = new double[9];
        public static double[] Daw_Blue_Y_Axie = new double[9];
        public static double[] Daw_White_Y_Axie = new double[9];
        private double Ya_Red_Target, Ya_Green_Target, Ya_Blue_Target, Ya_White_Target;//
        private int j;//取样循环                
        private void Raw_AutoMeasure_GrayRGBSelectAll_Enable(object sender, EventArgs e)//Gray Point 全点位测量；
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度

            if (btnTmp.Name == "btn_Red_Raw_Measure")//选择红色            
            {
                Dgv_Red_Raw_Measure.Rows.Clear();
                foreach (var series in chart_RGam_Curve.Series)
                {
                    series.Points.Clear();
                }
                for (j = 0; j < GrayRGBSelectAll.Length; j++)
                {
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = Convert.ToByte(GrayRGBSelectAll[j]);
                        Send_buf[4] = 0x00;
                        Send_buf[5] = 0x00;

                    }
                    CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
                    try
                    {
                        MainForm.comPort.Write(Send_buf, 0, 25);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }
                    Thread.Sleep(800);//上位机送入相应灰阶数据；
                    Msr_CA310();
                    Thread.Sleep(200);
                    Dgv_Red_Raw_Measure_Information(auto_lum, auto_x, auto_y);
                }

            }
            else if (btnTmp.Name == "btn_Green_Raw_Measure")//选择绿色
            {
                Dgv_Green_Raw_Measure.Rows.Clear();
                foreach (var series in chart_GGam_Curve.Series)
                {
                    series.Points.Clear();
                }
                for (j = 0; j < GrayRGBSelectAll.Length; j++)
                {
                    {

                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0x00;
                        Send_buf[4] = Convert.ToByte(GrayRGBSelectAll[j]);
                        Send_buf[5] = 0x00;

                    }
                    CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
                    try
                    {
                        MainForm.comPort.Write(Send_buf, 0, 25);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }
                    Thread.Sleep(1000);
                    Msr_CA310();
                    Dgv_Green_Raw_Measure_Information(auto_lum, auto_x, auto_y);
                }
            }
            else if (btnTmp.Name == "btn_Blue_Raw_Measure")//选择蓝色
            {
                Dgv_Blue_Raw_Measure.Rows.Clear();
                foreach (var series in chart_BGam_Curve.Series)
                {
                    series.Points.Clear();
                }

                for (j = 0; j < GrayRGBSelectAll.Length; j++)
                {
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0x00;
                        Send_buf[4] = 0x00;
                        Send_buf[5] = Convert.ToByte(GrayRGBSelectAll[j]);
                    }
                    CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
                    try
                    {
                        MainForm.comPort.Write(Send_buf, 0, 25);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }
                    Thread.Sleep(1000);
                    Msr_CA310();
                    Dgv_Blue_Raw_Measure_Information(auto_lum, auto_x, auto_y);
                }
            }
            else if (btnTmp.Name == "btn_White_Raw_Measure")//选择白色
            {
                Dgv_White_Raw_Measure.Rows.Clear();
                foreach (var series in chart_WGam_Curve.Series)
                {
                    series.Points.Clear();
                }

                for (j = 0; j < GrayRGBSelectAll.Length; j++)
                {
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = Convert.ToByte(GrayRGBSelectAll[j]);
                        Send_buf[4] = Convert.ToByte(GrayRGBSelectAll[j]);
                        Send_buf[5] = Convert.ToByte(GrayRGBSelectAll[j]);
                    }
                    CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
                    try
                    {
                        MainForm.comPort.Write(Send_buf, 0, 25);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }
                    Thread.Sleep(1000);
                    Msr_CA310();
                    Dgv_White_Raw_Measure_Information(auto_lum, auto_x, auto_y);
                }
            }
            else if (btnTmp.Name == "btn_Clear_RedGamma")
            {
                Dgv_Red_Raw_Measure.Rows.Clear();
                foreach (var series in chart_RGam_Curve.Series)
                {
                    series.Points.Clear();
                }
            }
            else if (btnTmp.Name == "btn_Clear_GreenGamma")
            {
                Dgv_Green_Raw_Measure.Rows.Clear();
                foreach (var series in chart_GGam_Curve.Series)
                {
                    series.Points.Clear();
                }
            }
            else if (btnTmp.Name == "btn_Clear_BlueGamma")
            {
                Dgv_Blue_Raw_Measure.Rows.Clear();
                foreach (var series in chart_BGam_Curve.Series)
                {
                    series.Points.Clear();
                }
            }
            else if (btnTmp.Name == "btn_Clear_WhiteGamma")
            {
                Dgv_White_Raw_Measure.Rows.Clear();
                foreach (var series in chart_WGam_Curve.Series)
                {
                    series.Points.Clear();
                }
            }
        }
        #endregion
        #region//==============RGB 三分量制表图==============
        private void Dgv_Red_Raw_Measure_Information(double Raw_Measure_Lv, double Raw_Measure_Xa, double Raw_Measure_Ya) //红色Raw数据显示
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;
            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.Dgv_Red_Raw_Measure.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.Dgv_Red_Raw_Measure.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.Dgv_Red_Raw_Measure.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.Dgv_Red_Raw_Measure.Rows[Row_Accum - 1].Selected = false;
                        this.Dgv_Red_Raw_Measure.Rows[Row_Accum].Selected = true;

                    }
                }
            }
            else
            {
                this.Dgv_Red_Raw_Measure.Rows.Add(Row1);
            }
            this.Dgv_Red_Raw_Measure.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;//序号
                                                                                    //Rows[x]为第x行,Cells[x]为第x列
            this.Dgv_Red_Raw_Measure.Rows[Row_Accum].Cells[1].Value = GrayRGBSelectAll[j];//GraySelectAll[i] 分别写入255/224/192/160/128/96/64/32
            this.Dgv_Red_Raw_Measure.Rows[Row_Accum].Cells[2].Value = Math.Round(Raw_Measure_Lv, 4);//需要获取到采集数据
            this.Dgv_Red_Raw_Measure.Rows[Row_Accum].Cells[3].Value = Math.Round(Raw_Measure_Xa, 4);
            this.Dgv_Red_Raw_Measure.Rows[Row_Accum].Cells[4].Value = Math.Round(Raw_Measure_Ya, 4);

            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.Dgv_Red_Raw_Measure.Rows.Count - 1;
                this.Dgv_Red_Raw_Measure.Rows[Current_Index].Selected = false;
                this.Dgv_Red_Raw_Measure.Rows[Row_Accum - 1].Selected = true;
            }
            Dgv_Red_Raw_Measure.FirstDisplayedScrollingRowIndex = Dgv_Red_Raw_Measure.RowCount - 1;
            Daw_Red_Y_Axie[j] = auto_lum;
            Ya_Red_Target = Daw_Red_Y_Axie[0];//数据不正确
            txt_target_RedLv.Text = Convert.ToString(Daw_Red_Y_Axie[0]);
            chart_RGam_Curve.Series[0].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Raw_Measure_Lv, 4));
            chart_RGam_Curve.Series[1].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.0) * Ya_Red_Target, 4));//Gamma2.0
            chart_RGam_Curve.Series[2].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.1) * Ya_Red_Target, 4));//Gamma2.1
            chart_RGam_Curve.Series[3].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.2) * Ya_Red_Target, 4));//Gamma2.2
            chart_RGam_Curve.Series[4].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.3) * Ya_Red_Target, 4));//Gamma2.3
            chart_RGam_Curve.Series[5].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.4) * Ya_Red_Target, 4));//Gamma2.4
        }
        private void Dgv_Green_Raw_Measure_Information(double Raw_Measure_Lv, double Raw_Measure_Xa, double Raw_Measure_Ya) //绿色Raw数据显示
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;
            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.Dgv_Green_Raw_Measure.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.Dgv_Green_Raw_Measure.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.Dgv_Green_Raw_Measure.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.Dgv_Green_Raw_Measure.Rows[Row_Accum - 1].Selected = false;
                        this.Dgv_Green_Raw_Measure.Rows[Row_Accum].Selected = true;

                    }
                }
            }
            else
            {
                this.Dgv_Green_Raw_Measure.Rows.Add(Row1);
            }
            this.Dgv_Green_Raw_Measure.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.Dgv_Green_Raw_Measure.Rows[Row_Accum].Cells[1].Value = GrayRGBSelectAll[j];//GraySelectAll[i] 分别写入255/224/192/160/96/64/32
            this.Dgv_Green_Raw_Measure.Rows[Row_Accum].Cells[2].Value = Math.Round(Raw_Measure_Lv, 4);//需要获取到采集数据
            this.Dgv_Green_Raw_Measure.Rows[Row_Accum].Cells[3].Value = Math.Round(Raw_Measure_Xa, 4);
            this.Dgv_Green_Raw_Measure.Rows[Row_Accum].Cells[4].Value = Math.Round(Raw_Measure_Ya, 4);

            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.Dgv_Green_Raw_Measure.Rows.Count - 1;
                this.Dgv_Green_Raw_Measure.Rows[Current_Index].Selected = false;
                this.Dgv_Green_Raw_Measure.Rows[Row_Accum - 1].Selected = true;
            }
            Dgv_Green_Raw_Measure.FirstDisplayedScrollingRowIndex = Dgv_Green_Raw_Measure.RowCount - 1;
            Daw_Green_Y_Axie[j] = auto_lum;
            Ya_Green_Target = Daw_Green_Y_Axie[0];
            txt_target_GreenLv.Text = Convert.ToString(Ya_Green_Target);
            chart_GGam_Curve.Series[0].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Raw_Measure_Lv, 4));
            chart_GGam_Curve.Series[1].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.0) * Ya_Green_Target, 4));//Gamma2.0
            chart_GGam_Curve.Series[2].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.1) * Ya_Green_Target, 4));//Gamma2.1
            chart_GGam_Curve.Series[3].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.2) * Ya_Green_Target, 4));//Gamma2.2
            chart_GGam_Curve.Series[4].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.3) * Ya_Green_Target, 4));//Gamma2.3
            chart_GGam_Curve.Series[5].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.4) * Ya_Green_Target, 4));//Gamma2.4 

        }
        private void Dgv_Blue_Raw_Measure_Information(double Raw_Measure_Lv, double Raw_Measure_Xa, double Raw_Measure_Ya) //蓝色Raw数据显示
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;
            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.Dgv_Blue_Raw_Measure.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.Dgv_Blue_Raw_Measure.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.Dgv_Blue_Raw_Measure.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.Dgv_Blue_Raw_Measure.Rows[Row_Accum - 1].Selected = false;
                        this.Dgv_Blue_Raw_Measure.Rows[Row_Accum].Selected = true;

                    }
                }
            }
            else
            {
                this.Dgv_Blue_Raw_Measure.Rows.Add(Row1);
            }
            this.Dgv_Blue_Raw_Measure.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.Dgv_Blue_Raw_Measure.Rows[Row_Accum].Cells[1].Value = GrayRGBSelectAll[j];//GraySelectAll[i] 分别写入255/224/192/160/96/64/32
            this.Dgv_Blue_Raw_Measure.Rows[Row_Accum].Cells[2].Value = Math.Round(Raw_Measure_Lv, 4);//需要获取到采集数据
            this.Dgv_Blue_Raw_Measure.Rows[Row_Accum].Cells[3].Value = Math.Round(Raw_Measure_Xa, 4);
            this.Dgv_Blue_Raw_Measure.Rows[Row_Accum].Cells[4].Value = Math.Round(Raw_Measure_Ya, 4);

            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.Dgv_Blue_Raw_Measure.Rows.Count - 1;
                this.Dgv_Blue_Raw_Measure.Rows[Current_Index].Selected = false;
                this.Dgv_Blue_Raw_Measure.Rows[Row_Accum - 1].Selected = true;
            }
            Dgv_Blue_Raw_Measure.FirstDisplayedScrollingRowIndex = Dgv_Blue_Raw_Measure.RowCount - 1;
            Daw_Blue_Y_Axie[j] = auto_lum;
            Ya_Blue_Target = Daw_Blue_Y_Axie[0];
            txt_target_BlueLv.Text = Convert.ToString(Daw_Blue_Y_Axie[0]);
            chart_BGam_Curve.Series[0].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Raw_Measure_Lv, 4));
            chart_BGam_Curve.Series[1].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.0) * Ya_Blue_Target, 4));//Gamma2.0
            chart_BGam_Curve.Series[2].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.1) * Ya_Blue_Target, 4));//Gamma2.1
            chart_BGam_Curve.Series[3].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.2) * Ya_Blue_Target, 4));//Gamma2.2
            chart_BGam_Curve.Series[4].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.3) * Ya_Blue_Target, 4));//Gamma2.3
            chart_BGam_Curve.Series[5].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.4) * Ya_Blue_Target, 4));//Gamma2.4
        }
        private void Dgv_White_Raw_Measure_Information(double Raw_Measure_Lv, double Raw_Measure_Xa, double Raw_Measure_Ya) //白色Raw数据显示
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;
            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.Dgv_White_Raw_Measure.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.Dgv_White_Raw_Measure.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.Dgv_White_Raw_Measure.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.Dgv_White_Raw_Measure.Rows[Row_Accum - 1].Selected = false;
                        this.Dgv_White_Raw_Measure.Rows[Row_Accum].Selected = true;

                    }
                }
            }
            else
            {
                this.Dgv_White_Raw_Measure.Rows.Add(Row1);
            }
            this.Dgv_White_Raw_Measure.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.Dgv_White_Raw_Measure.Rows[Row_Accum].Cells[1].Value = GrayRGBSelectAll[j];//GraySelectAll[i] 分别写入255/224/192/160/96/64/32
            this.Dgv_White_Raw_Measure.Rows[Row_Accum].Cells[2].Value = Math.Round(Raw_Measure_Lv, 4);//需要获取到采集数据
            this.Dgv_White_Raw_Measure.Rows[Row_Accum].Cells[3].Value = Math.Round(Raw_Measure_Xa, 4);
            this.Dgv_White_Raw_Measure.Rows[Row_Accum].Cells[4].Value = Math.Round(Raw_Measure_Ya, 4);

            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.Dgv_White_Raw_Measure.Rows.Count - 1;
                this.Dgv_White_Raw_Measure.Rows[Current_Index].Selected = false;
                this.Dgv_White_Raw_Measure.Rows[Row_Accum - 1].Selected = true;
            }
            Dgv_White_Raw_Measure.FirstDisplayedScrollingRowIndex = Dgv_White_Raw_Measure.RowCount - 1;
            Daw_White_Y_Axie[j] = auto_lum;
            Ya_White_Target = Daw_White_Y_Axie[0];
            txt_target_WhiteLv.Text = Convert.ToString(Daw_White_Y_Axie[0]);
            chart_WGam_Curve.Series[0].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Raw_Measure_Lv, 4));
            chart_WGam_Curve.Series[1].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.0) * Ya_White_Target, 4));//Gamma2.0
            chart_WGam_Curve.Series[2].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.1) * Ya_White_Target, 4));//Gamma2.1
            chart_WGam_Curve.Series[3].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.2) * Ya_White_Target, 4));//Gamma2.2
            chart_WGam_Curve.Series[4].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.3) * Ya_White_Target, 4));//Gamma2.3
            chart_WGam_Curve.Series[5].Points.AddXY(GrayRGBSelectAll[j], Math.Round(Math.Pow(Convert.ToDouble(GrayRGBSelectAll[j]) / 255, 2.4) * Ya_White_Target, 4));//Gamma2.4
        }
        #endregion
        #region//============RGB 三分量图放大缩小============//暂时屏蔽子窗体开启标识
        private void DemuraAutoMeasure_VisibleChanged(object sender, EventArgs e)
        {
            //if (this.Visible == true) //子窗体已开启标识
            //{
            //    MainForm.comPort.Close();
            //    Thread.Sleep(5);
            //    MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);

            //    Thread.Sleep(5);
            //    MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);//自定义串口时修改
            //    Thread.Sleep(5);
            //    MainForm.comPort.Open();
            //    MainForm.textBox1.Text += "Demura界面com 已经连接！\r\n";
            //}
            //else
            //{
            //    //MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);

            //}
        }
        private void DemuraAutoMeasure_FormClosing(object sender, FormClosingEventArgs e)
        {
            MainForm.btn_AutoMeasure.Enabled = true;
            MainForm.textBox1.Text += "Demura界面com 已经断开！\r\n";
        }
        public static double[] Dgv_Red_Raw_Ya = new double[GrayRGBSelectAll.Length];
        public static double[] Dgv_Green_Raw_Ya = new double[GrayRGBSelectAll.Length];
        public static double[] Dgv_Blue_Raw_Ya = new double[GrayRGBSelectAll.Length];
        public static double[] Dgv_White_Raw_Ya = new double[GrayRGBSelectAll.Length];
        private void chart_RGam_Curve_DoubleClick(object sender, EventArgs e)
        {
            Raw_R_Measure_Frm Frm_R = new Raw_R_Measure_Frm();
            for (int i = 0; i < GrayRGBSelectAll.Length; i++)
            {
                Dgv_Red_Raw_Ya[i] = Daw_Red_Y_Axie[i];//数据传递
            }
            Frm_R.Show();
        }
        private void 另存为ToolStripMenuItem1_Click(object sender, EventArgs e)//Raw_Red另存为
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

            string str = "No.,Gray,Lv,X,Y\r\n";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    for (int i = 0; i < (this.Dgv_Red_Raw_Measure.Rows.Count - 1); i++)
                    {
                        str = str + this.Dgv_Red_Raw_Measure.Rows[i].Cells[0].Value.ToString() + ","
                            + this.Dgv_Red_Raw_Measure.Rows[i].Cells[1].Value.ToString() + ","
                            + this.Dgv_Red_Raw_Measure.Rows[i].Cells[2].Value.ToString() + ","
                            + this.Dgv_Red_Raw_Measure.Rows[i].Cells[3].Value.ToString() + ","
                            + this.Dgv_Red_Raw_Measure.Rows[i].Cells[4].Value.ToString() + "\r\n";

                    }
                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Raw_Red测量数据保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Raw_Red测量保存时发生错误，请再试！", "系统提示：");
                }
            }
        }
        private void chart_GGam_Curve_DoubleClick(object sender, EventArgs e)
        {
            Raw_G_Measure_Frm Frm_G = new Raw_G_Measure_Frm();
            for (int i = 0; i < GrayRGBSelectAll.Length; i++)
            {
                Dgv_Green_Raw_Ya[i] = Daw_Green_Y_Axie[i];//数据传递
            }
            Frm_G.Show();
        }
        private void 另存为ToolStripMenuItem2_Click(object sender, EventArgs e)//Raw_Green另存为
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

            string str = "No.,Gray,Lv,X,Y\r\n";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    for (int i = 0; i < (this.Dgv_Green_Raw_Measure.Rows.Count - 1); i++)
                    {
                        str = str + this.Dgv_Green_Raw_Measure.Rows[i].Cells[0].Value.ToString() + ","
                            + this.Dgv_Green_Raw_Measure.Rows[i].Cells[1].Value.ToString() + ","
                            + this.Dgv_Green_Raw_Measure.Rows[i].Cells[2].Value.ToString() + ","
                            + this.Dgv_Green_Raw_Measure.Rows[i].Cells[3].Value.ToString() + ","
                            + this.Dgv_Green_Raw_Measure.Rows[i].Cells[4].Value.ToString() + "\r\n";

                    }
                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Raw_Green测量数据保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Raw_Green测量保存时发生错误，请再试！", "系统提示：");
                }
            }
        }
        private void chart_BGam_Curve_DoubleClick(object sender, EventArgs e)
        {

            Raw_B_Measure_Frm Frm_B = new Raw_B_Measure_Frm();
            for (int i = 0; i < GrayRGBSelectAll.Length; i++)
            {
                Dgv_Blue_Raw_Ya[i] = Daw_Blue_Y_Axie[i];//数据传递
            }
            Frm_B.Show();

        }
        private void 另存为ToolStripMenuItem3_Click(object sender, EventArgs e)//Raw_Blue另存为
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

            string str = "No.,Gray,Lv,X,Y\r\n";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    for (int i = 0; i < (this.Dgv_Blue_Raw_Measure.Rows.Count - 1); i++)
                    {
                        str = str + this.Dgv_Blue_Raw_Measure.Rows[i].Cells[0].Value.ToString() + ","
                            + this.Dgv_Blue_Raw_Measure.Rows[i].Cells[1].Value.ToString() + ","
                            + this.Dgv_Blue_Raw_Measure.Rows[i].Cells[2].Value.ToString() + ","
                            + this.Dgv_Blue_Raw_Measure.Rows[i].Cells[3].Value.ToString() + ","
                            + this.Dgv_Blue_Raw_Measure.Rows[i].Cells[4].Value.ToString() + "\r\n";

                    }
                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Raw_Blue测量数据保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Raw_Blue测量保存时发生错误，请再试！", "系统提示：");
                }
            }
        }
        private void chart_WGam_Curve_DoubleClick(object sender, EventArgs e)
        {

            Raw_W_Measure_Frm Frm_W = new Raw_W_Measure_Frm();
            for (int i = 0; i < GrayRGBSelectAll.Length; i++)
            {
                Dgv_White_Raw_Ya[i] = Daw_White_Y_Axie[i];//数据传递
            }
            Frm_W.Show();

        }
        private void 另存为ToolStripMenuItem4_Click(object sender, EventArgs e)//Raw_White另存为
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

            string str = "No.,Gray,Lv,X,Y\r\n";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    for (int i = 0; i < (this.Dgv_White_Raw_Measure.Rows.Count - 1); i++)
                    {
                        str = str + this.Dgv_White_Raw_Measure.Rows[i].Cells[0].Value.ToString() + ","
                            + this.Dgv_White_Raw_Measure.Rows[i].Cells[1].Value.ToString() + ","
                            + this.Dgv_White_Raw_Measure.Rows[i].Cells[2].Value.ToString() + ","
                            + this.Dgv_White_Raw_Measure.Rows[i].Cells[3].Value.ToString() + ","
                            + this.Dgv_White_Raw_Measure.Rows[i].Cells[4].Value.ToString() + "\r\n";

                    }
                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Raw_White测量数据保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Raw_White测量保存时发生错误，请再试！", "系统提示：");
                }
            }
        }
        #endregion
        /****************CIE 色域匹配****************/
        private void char_CIE_Gamut_Typical_Data()
        {
            chart_CIE.Series[0].Points.Clear();
        }
        private void btn_CIE_Clear_Click(object sender, EventArgs e)
        {
            chart_CIE.Series[0].Points.Clear();
        }

        private double Area;
        public double ColorGamut_Calc(double Rx, double Ry, double Gx, double Gy, double Bx, double By)
        {
            return Area = Math.Abs(Rx * (Gy - By) + Gx * (By - Ry) + Bx * (Ry - Gy)) / 0.1582;
        }
        private void btn_CIE_Measure_Click(object sender, EventArgs e)
        {
            double[] User_point = new double[6];
            MainForm.OLED_Show_Click(MainForm.btnred, e);
            Thread.Sleep(400);
            Msr_CA310();
            //测量建立表格
            Thread.Sleep(100);
            txt_R_Point_X.Text = Convert.ToString(Math.Round(auto_x, 4));
            txt_R_Point_Y.Text = Convert.ToString(Math.Round(auto_y, 4));
            chart_CIE.Series[0].Points.AddXY(Math.Round(Convert.ToDouble(txt_R_Point_X.Text), 4), Math.Round(Convert.ToDouble(txt_R_Point_Y.Text), 4));
            User_point[0] = auto_x; User_point[1] = auto_y;
            Thread.Sleep(100);

            MainForm.OLED_Show_Click(MainForm.btngreen, e);
            Thread.Sleep(400);
            Msr_CA310();
            //测量建立表格
            Thread.Sleep(100);
            txt_G_Point_X.Text = Convert.ToString(Math.Round(auto_x, 4));
            txt_G_Point_Y.Text = Convert.ToString(Math.Round(auto_y, 4));
            chart_CIE.Series[0].Points.AddXY(Math.Round(Convert.ToDouble(txt_G_Point_X.Text), 4), Math.Round(Convert.ToDouble(txt_G_Point_Y.Text), 4));
            User_point[2] = auto_x; User_point[3] = auto_y;
            Thread.Sleep(100);

            MainForm.OLED_Show_Click(MainForm.btnblue, e);
            Thread.Sleep(400);
            Msr_CA310();
            //测量建立表格
            Thread.Sleep(100);
            txt_B_Point_X.Text = Convert.ToString(Math.Round(auto_x, 4));
            txt_B_Point_Y.Text = Convert.ToString(Math.Round(auto_y, 4));
            chart_CIE.Series[0].Points.AddXY(Math.Round(Convert.ToDouble(txt_B_Point_X.Text), 4), Math.Round(Convert.ToDouble(txt_B_Point_Y.Text), 4));
            chart_CIE.Series[0].Points.AddXY(Math.Round(Convert.ToDouble(txt_R_Point_X.Text), 4), Math.Round(Convert.ToDouble(txt_R_Point_Y.Text), 4));
            User_point[4] = auto_x; User_point[5] = auto_y;
            double NTSC_Calc = Math.Round(ColorGamut_Calc(User_point[0], User_point[1], User_point[2], User_point[3], User_point[4], User_point[5]) / 2, 4) * 100;
            double sRGB_Calc = Math.Round(ColorGamut_Calc(User_point[0], User_point[1], User_point[2], User_point[3], User_point[4], User_point[5]) / 1.416561, 4) * 100;
            double DCIP3_Calc = Math.Round(ColorGamut_Calc(User_point[0], User_point[1], User_point[2], User_point[3], User_point[4], User_point[5]) / 1.921618, 4) * 100;
            Thread.Sleep(100);
            MainForm.GrayShow(0, 0, 0);
            txt_Calc_NTSC.Text = Convert.ToString(NTSC_Calc) + "%";
            txt_Calc_sRGB.Text = Convert.ToString(sRGB_Calc) + "%";
            txt_Calc_DCIP3.Text = Convert.ToString(DCIP3_Calc) + "%";
        }


    }
}
